package com.lantosec.hmrp.modules.supplier.controller.app;


import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.mvc.BaseController;
import com.lantosec.hmrp.modules.supplier.entity.*;
import com.lantosec.hmrp.modules.supplier.service.ISupplierAccessoryService;
import com.lantosec.hmrp.modules.supplier.service.ISupplierInfoService;
import com.lantosec.hmrp.modules.supplier.service.ISupplierLinkmanService;
import com.lantosec.hmrp.modules.supplier.vo.SupplierAccessoryVO;
import com.lantosec.hmrp.modules.supplier.vo.SupplierInfoVO;
import com.lantosec.hmrp.modules.supplier.vo.SupplierLinkmanVO;
import com.lantosec.hmrp.modules.uploads.entity.SysUploads;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 *
 * 供应商信息 控制器
 *
 * @author AutoCode
 * @since 2020-02-20
 */
@Api(value = "供应商信息控制器APP端", tags = "供应商信息控制器APP端")
@RestController
@RequestMapping("/supplierInfoApp")
@Log4j2
public class SupplierInfoAppController extends BaseController {
    @Autowired
    private ISupplierInfoService service;

    @Autowired
    private ISupplierLinkmanService iSupplierLinkmanService;

    @Autowired
    private ISupplierAccessoryService iSupplierAccessoryService;

    @GetMapping("querySupplierCode")
    @ApiOperation(value = "查询相对应供应商类型下的供应商编码")
    public Result<PageVO<YyGyslist>> querySupplierCode(PageVO<YyGyslist> page,@ApiParam(value = "供应商类型") @RequestParam(name = "typeId",required = false) String typeId,
                                                       @ApiParam(value = "供应商名称") @RequestParam(name = "name",required = false) String name){
        return Result.ok(service.querySupplierCode(page,typeId,name));
    }

    @GetMapping("querySupplierType")
    @ApiOperation(value = "查询全部供应商类型")
    public Result<List<YyGyslist>> querySupplierType(){
        return Result.ok(service.querySupplierType());
    }

    @GetMapping("queryAll")
    @ApiOperation(value = "查询系统中全部供应商编码")
    public Result<List<SupplierInfo>> queryAll(){
        return Result.ok(service.queryAll());
    }

    @GetMapping("getSupplierEntryAll")
    @ApiOperation(value = "查询系统中全部已准入的供应商")
    public Result<List<SupplierInfo>> getSupplierEntryAll(){
        return Result.ok(service.getSupplierEntryAll());
    }

    @GetMapping("querySupplierInfoAll")
    @ApiOperation(value = "分页查询全部供应商信息")
    public Result<PageVO<SupplierInfoVO>> querySupplierInfoAll(@ApiParam(value = "分页对象", required = true) PageVO<SupplierInfo> page,
                                                             @ApiParam(value = "供应商名称") @RequestParam(name = "supplierName",required = false) String supplierName,
                                                             @ApiParam(value = "供应商类型") @RequestParam(name = "supplierType",required = false) String supplierType,
                                                             @ApiParam(value = "供应商状态") @RequestParam(name = "entryState",required = false) String entryState){
        return Result.ok(service.querySupplierInfoAll(page,supplierName,supplierType,entryState));
    }

    @GetMapping("querySupplierInfoByDeptId")
    @ApiOperation(value = "分页查询本部门下的供应商信息")
    public Result<PageVO<SupplierInfoVO>> querySupplierInfoByDeptId(@ApiParam(value = "分页对象", required = true) PageVO<SupplierInfo> page,
                                                                    @ApiParam(value = "供应商名称") @RequestParam(name = "supplierName",required = false) String supplierName,
                                                                    @ApiParam(value = "供应商类型") @RequestParam(name = "supplierType",required = false) String supplierType,
                                                                    @ApiParam(value = "当前用户名id") @RequestParam(name = "userId",required = true) String userId){
        String deptId = service.getDeptId(userId);
        if(StringUtils.isEmpty(deptId)){
            return Result.fail("当前用户没有所属部门");
        }
        return Result.ok(service.querySupplierInfoByDept(page,supplierName,supplierType,deptId));
    }

    @GetMapping("getSupplierCodeCount")
    @ApiOperation(value = "新增供应商判断系统是否已经存在")
        public Result<String> getSupplierCode(@ApiParam(value = "supplierCode", required = true) @RequestParam(name="supplierCode") String supplierCode) {
        //查询系统中是否已存在此供应商
        Result result = service.getSupplierCode(supplierCode);
        return result;
    }

    @GetMapping("querySupplierInfoById")
    @ApiOperation(value = "查询供应商的详情")
    public Result<SupplierInfoVO> querySupplierInfoById(@ApiParam(value = "supplierCode", required = true) @RequestParam(name="supplierCode") String supplierCode,
                                                        @ApiParam(value = "entryState", required = true) @RequestParam(name="entryState") String entryState){
        SupplierInfoVO supplierInfo = service.querySupplierInfoById(supplierCode,entryState);
        return Result.ok(supplierInfo);
    }

    @PostMapping("insertSupplierInfo")
    @ApiOperation(value = "新增供应商")
    public Result<String> insertSupplierInfo(@ApiParam(value = "供应商信息组装对象", required = true) @RequestBody @Validated Supplier supplier){
        String supplierCode = service.insertSupplierInfo(supplier);
        return Result.ok(supplierCode);
    }


    @GetMapping("getSupplierInfo")
    @ApiOperation("查询供应商和联系人和附件基本信息")
    public Result<Supplier> getSupplierInfo(@ApiParam(value = "supplierCode", required = true) @RequestParam(name="supplierCode") String supplierCode,
                                            @ApiParam(value = "supplierCode", required = true) @RequestParam(name="entryState") String entryState){
        SysUploads sysUploads = new SysUploads();
        Supplier supplier = new Supplier();
        //查询supplierCode所对应的供应商信息
        SupplierInfoVO supplierInfo = service.querySupplierInfoById(supplierCode,entryState);
        supplier.setSupplierInfo(supplierInfo);

        //查询supplierCode所对应的供应商的联系人信息
        SupplierLinkmanVO supplierLinkmanVO = iSupplierLinkmanService.getSavePath(supplierInfo.getLinkmanId());
        supplier.setSupplierLinkman(supplierLinkmanVO);

        //查询supplierCode所对应的附件信息
        List<SupplierAccessoryVO> list = iSupplierAccessoryService.querySupplierAccessoryPath(supplierCode);
        supplier.setSupplierAccessory(list);
        return Result.ok(supplier);
    }

    @PutMapping("updateSupplierInfoAll")
    @ApiOperation(value = "编辑供应商信息")
    public Result<Void> updateSupplierInfoAll(@ApiParam(value = "供应商信息VO对象", required = true) @RequestBody @Validated Supplier supplier){
        service.updateSupplierInfoAll(supplier);
        return Result.ok();
    }

    @PutMapping("updateSupplierInfo")
    @ApiOperation(value = "供应商信息变更")
    public Result<Void> updateSupplierInfo(@ApiParam(value = "供应商基本信息对象", required = true) @RequestBody SupplierInfoVO vo){
        SupplierInfo entity = vo.convertToEntity();
        //修改供应商基本信息
        service.updateById(entity);
        return Result.ok();
    }

    @GetMapping("getSupplierType")
    @ApiOperation(value = "新增供应商类型前查询供应商类型是否存在，存在则不允许增加")
    public Result<String> getSupplierType(@ApiParam(value = "supplierType", required = true) @RequestParam(name="supplierType") String supplierType){
        //查询basicdata.yy_gyslist表中是否已存在此供应商类型
        Integer count = service.getSupplierType(supplierType);
        if(count>=1){
            return Result.ok("供应商类型已存在");
        }
        return Result.ok("供应商类型不存在");
    }

    @PostMapping("insertSupplierInfoType")
    @ApiOperation(value = "新增供应商类型")
    public Result<String> insertSupplierInfoType(@ApiParam(value = "供应商信息组装对象", required = true) @RequestBody @Validated Supplier supplier){
        String supplierCode = service.insertSupplierInfoType(supplier);
        return Result.ok(supplierCode);
    }

    @GetMapping("getSupplierTypeNum")
    @ApiOperation(value = "查询供应商各个类型的分类数")
    public Result<List<SupplierTypeNum>> getSupplierTypeNum(){
        List<SupplierTypeNum> supplierTypeNumList = service.getSupplierTypeNum();
        return Result.ok(supplierTypeNumList);
    }

    @GetMapping("getSupplierEntryNum")
    @ApiOperation(value = "查询供应商已准入/未准入数")
    public Result<List<SupplierEntryNum>> getSupplierEntryNum(){
        List<SupplierEntryNum> supplierEntryNumList = service.getSupplierEntryNum();
        return Result.ok(supplierEntryNumList);
    }

    @GetMapping("getSupplierBadNum")
    @ApiOperation(value = "查询供应商不良记录数")
    public Result<List<SupplierBadNum>> getSupplierBadNum(){
        List<SupplierBadNum> supplierBadNumList = service.getSupplierBadNum();
        return Result.ok(supplierBadNumList);
    }

    @DeleteMapping(value = "delete/{ids}")
    @ApiOperation("逻辑删除")
    public Result<Void> delete(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name="ids") String ids){
        if(StringUtils.isNotEmpty(ids)){
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }

    @GetMapping("getHisMaterialDir")
    @ApiOperation(value = "查询物资目录列表")
    public Result<PageVO<HisMaterialDir>> getHisMaterialDir(@ApiParam(value = "分页对象", required = true) PageVO<HisMaterialDir> page,
                                                            @ApiParam(value = "供应商编码", required = true) @RequestParam(name = "supplierCode") String supplierCode,
                                                            @ApiParam(value = "物资产品代码") @RequestParam(name = "materialId",required = false) String materialId,
                                                            @ApiParam(value = "物资产品名称") @RequestParam(name = "materialName",required = false) String materialName,
                                                            @ApiParam(value = "生产厂家") @RequestParam(name = "factory",required = false) String factory){
        page = service.getHisMaterialDir(page,supplierCode,materialId,materialName,factory);
        return Result.ok(page);
    }

    @GetMapping("getHisDrugDir")
    @ApiOperation(value = "查询药品目录列表")
    public Result<PageVO<HisDrugDir>> getHisDrugDir(@ApiParam(value = "分页对象", required = true) PageVO<HisDrugDir> page,
                                                    @ApiParam(value = "供应商编码", required = true) @RequestParam(name = "supplierCode") String supplierCode,
                                                    @ApiParam(value = "药品产品代码") @RequestParam(name = "drugId",required = false) String drugId,
                                                    @ApiParam(value = "药品产品名称") @RequestParam(name = "drugName",required = false) String drugName,
                                                    @ApiParam(value = "生产厂家") @RequestParam(name = "factory",required = false) String factory){
        page = service.getHisDrugDir(page,supplierCode,drugId,drugName,factory);
        return Result.ok(page);
    }

    @GetMapping("getHisInboundPay")
    @ApiOperation(value = "查询供应商业务信息列表")
    public Result<PageVO<HisInboundPay>> getHisInboundPay(@ApiParam(value = "分页对象", required = true) PageVO<HisInboundPay> page,
                                                          @ApiParam(value = "供应商编码", required = true) @RequestParam(name = "supplierCode") String supplierCode,
                                                          @ApiParam(value = "类型(0耗材 1药品)") @RequestParam(name = "mainType",required = false) Integer mainType,
                                                          @ApiParam(value = "货物名称") @RequestParam(name = "goodsName",required = false) String goodsName,
                                                          @ApiParam(value = "生厂厂家") @RequestParam(name = "factoryName",required = false) String factoryName){
        page = service.getHisInboundPay(page,supplierCode,mainType,goodsName,factoryName);
        return Result.ok(page);
    }

    @GetMapping("getSupplierCodeType")
    @ApiOperation(value = "查询供应商类型")
    public Result<List<String>> getSupplierCodeType(@ApiParam(value = "supplierCode", required = true) @RequestParam(name="supplierCode") String supplierCode){
        List<String> supplierTypes = service.getSupplierCodeType(supplierCode);
        return Result.ok(supplierTypes);
    }
}
